﻿using System;

namespace Ucto.ToExcelConvertor
{
	public class DataProcessor
	{
		public DataProcessor(IDataReader dataReader, IValueHandler valueHandler)
		{
			if (dataReader == null)
			{
				throw new ArgumentNullException("dataReader");
			}
			if (valueHandler == null)
			{
				throw new ArgumentNullException("valueHandler");
			}

			this.DataReader = dataReader;
			this.ValueHandler = valueHandler;
		}

		public IValueHandler ValueHandler { get; set; }

		protected IDataReader DataReader { get; private set; }

		public void Process()
		{
			try
			{
				string value = null;

				while (!string.IsNullOrEmpty(value = this.DataReader.GetNextValue()))
				{
					this.ValueHandler.Handle(value);
				}
			}
			catch (Exception ex)
			{
				throw new Exception("Error while processing data.", ex);
			}
		}
	}
}
